﻿//-----------------------------------------------------------------------------
//<filedescription file="ArrayBuilder.LogicalDriveSettingsDialog.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     A dialog within the Array Builder UI framework that allows the user
//     to define settings specific to a logical drive.
//
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using System.Globalization;
using System.Reflection;
using System.Xml;
using Microsoft.ConfigurationManagement.IntegrationDevelopmentKit;

namespace Microsoft.ConfigurationManagement.OemDeployment.ArrayBuilder
{
    public partial class LogicalDriveSettingsDialog : Form
    {

        #region Properties

        //ArrayBuilderUtilities Utilities = new ArrayBuilderUtilities();

        /// <summary>
        /// 
        /// </summary>
        public string DriveSize
        {
            get { return textBoxGigabytes.Text; }
            set { textBoxGigabytes.Text = value; }
        }

        /// <summary>
        ///     
        /// </summary>
        /// <value>
        ///     <para>
        ///         
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public bool DrivePercentEnabled
        {
            get { return radioButtonCreateDrivePercent.Enabled; }
            set
            {
                if (value == false)
                {
                    radioButtonCreateDrivePercent.Checked = false;
                    radioButtonCreateDrivePercent.Enabled = false;
                    numericUpDownPercent.Enabled = false;
                    label1.Enabled = false;
                    labelPercentRemaining.Enabled = false;
                    //radioButtonCreateDrivePercent.Visible = false;
                    //numericUpDownPercent.Visible = false;
                    //label1.Visible = false;
                    labelPercentRemaining.Visible = false;
                }
                else
                {
                    radioButtonCreateDrivePercent.Visible = true;
                    numericUpDownPercent.Visible = true;
                    label1.Visible = true;
                    labelPercentRemaining.Visible = true;
                    radioButtonCreateDrivePercent.Enabled = true;
                    numericUpDownPercent.Enabled = true;
                    label1.Enabled = true;
                    labelPercentRemaining.Enabled = true;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string DrivePercent
        {
            get { return numericUpDownPercent.Value.ToString(CultureInfo.CurrentUICulture); }
            set 
            {
                decimal percent;
                if (decimal.TryParse(value, out percent))
                {
                    numericUpDownPercent.Value = percent;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public decimal DrivePercentLimit
        {
            get { return numericUpDownPercent.Maximum; }
            set 
            { 
                numericUpDownPercent.Maximum = value;
                labelPercentRemaining.Text = string.Format(Properties.Resources.LogicalDriveSettings_PercentRemaining, value.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool UsesExactSize
        {
            get { return radioButtonSizeExact.Checked; }
            set 
            { 
                radioButtonSizeExact.Checked = value;
                if (value == true)
                {
                    textBoxGigabytes.Enabled = true;
                    numericUpDownPercent.Enabled = false;
                    labelPercentRemaining.Enabled = false;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool UsesPercentage
        {
            get { return radioButtonCreateDrivePercent.Checked; }
            set 
            { 
                radioButtonCreateDrivePercent.Checked = value;
                if (value == true)
                {
                    textBoxGigabytes.Enabled = false;
                    numericUpDownPercent.Enabled = true;
                    labelPercentRemaining.Enabled = true;
                    labelPercentRemaining.Visible = true;
                }
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public bool UsesMaximumEnabled
        {
            get {return radioButtonCreateDriveMax.Enabled;}
            set { 
                radioButtonCreateDriveMax.Enabled = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool UsesMaximum
        {
            get { return radioButtonCreateDriveMax.Checked; }
            set 
            { 
                radioButtonCreateDriveMax.Checked = value;
                if (value == true)
                {
                    textBoxGigabytes.Enabled = false;
                    numericUpDownPercent.Enabled = false;
                }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public bool OptionsGridEnabled
        {
            get { return dataGridViewAdditionalOptions.Enabled; }
            set
            {
                dataGridViewAdditionalOptions.Enabled = value;
                if (value == false)
                {
                    groupBoxAdditionalOptions.Visible = false;
                    dataGridViewAdditionalOptions.Visible = false;
                    this.Height = this.Height - 145;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public XmlNode OptionsValues
        {
            get
            {
                if (dataGridViewAdditionalOptions.Enabled == true)
                {
                    if (dataGridViewAdditionalOptions.RowCount > 0)
                    {
                        return Utilities.GetOptionsDataGridValues(dataGridViewAdditionalOptions, "LogicalDriveOptions");
                    } 
                }
                return null;
            }
            set
            {
                dataGridViewAdditionalOptions = Utilities.FillOptionsDataGrid(dataGridViewAdditionalOptions, OemOptionsNode, value);
                //FillOptionsDataGrid(value); 
            }
        }


        private XmlNode oemOptionsNode;
        /// <summary>
        /// 
        /// </summary>
        public XmlNode OemOptionsNode
        {
            get { return oemOptionsNode; }
            set { oemOptionsNode = value; }
        }
        


        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public LogicalDriveSettingsDialog()
        {
            InitializeComponent();
            SetupErrorProvider();
            this.TopMost = true;
        }


        #endregion

        #region Validation

        private void SetupErrorProvider()
        {
            epGigabytes = new ErrorProvider();
            epGigabytes.SetIconAlignment(textBoxGigabytes, ErrorIconAlignment.MiddleRight);
            epGigabytes.SetIconPadding(textBoxGigabytes, 2);
            epGigabytes.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            epPercent = new ErrorProvider();
            epPercent.SetIconAlignment(numericUpDownPercent, ErrorIconAlignment.MiddleRight);
            epPercent.SetIconPadding(numericUpDownPercent, 2);
            epPercent.BlinkStyle = ErrorBlinkStyle.NeverBlink;

            epAdditionalOptions = new ErrorProvider();
            epAdditionalOptions.SetIconAlignment(dataGridViewAdditionalOptions, ErrorIconAlignment.MiddleRight);
            epAdditionalOptions.SetIconPadding(dataGridViewAdditionalOptions, 2);
            epAdditionalOptions.BlinkStyle = ErrorBlinkStyle.NeverBlink;

        }

        private void ValidateGigabytes()
        {

            try
            {
                if (string.IsNullOrEmpty(textBoxGigabytes.Text) == true) 
                {
                    epGigabytes.SetError(textBoxGigabytes, Properties.Resources.ArrayBuilder_Error_CannotBeBlank);
                }
                else
                {
                    int size = 0;
                    if (int.TryParse(textBoxGigabytes.Text, out size))
                    {
                        epGigabytes.SetError(textBoxGigabytes, "");
                    }
                    else
                    {
                        epGigabytes.SetError(textBoxGigabytes, Properties.Resources.ArrayBuilder_Error_NotNumeric);
                    }
                }
            }
            catch (Exception ex)
            {
                Utilities.ShowExceptionMessage(ex, MethodBase.GetCurrentMethod());
                
            }
        }

        private void ValidatePercent()
        {

            try
            {
                if ((numericUpDownPercent.Value <  1) || (numericUpDownPercent.Value > 100))
                {
                    epPercent.SetError(numericUpDownPercent, Properties.Resources.ArrayBuilder_Error_ValueBetween1And100);
                }
                else
                {
                    epPercent.SetError(numericUpDownPercent, "");
                }
            }
            catch (Exception ex)
            {
                Utilities.ShowExceptionMessage(ex, MethodBase.GetCurrentMethod());
                
            }
        }

        private void ValidateDataGrid()
        {

        }

        
       


        #endregion

        #region Event Handlers


        private void radioButtonSizeExact_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonSizeExact.Checked == true)
            {
                textBoxGigabytes.Enabled = true;
                numericUpDownPercent.Enabled = false;
                labelPercentRemaining.Enabled = false;
            }
        }

        private void radioButtonCreateDrivePercent_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonCreateDrivePercent.Checked == true)
            {
                numericUpDownPercent.Enabled = true;
                textBoxGigabytes.Enabled = false;
                labelPercentRemaining.Visible = true;
                labelPercentRemaining.Enabled = true;
                decimal currentMax = numericUpDownPercent.Maximum - numericUpDownPercent.Value;
                labelPercentRemaining.Text = string.Format(Properties.Resources.LogicalDriveSettings_PercentRemaining, currentMax.ToString());
            }
        }

        private void textBoxGigabytes_TextChanged(object sender, EventArgs e)
        {
            ValidateGigabytes();
        }

        private void radioButtonCreateDriveMax_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonCreateDriveMax.Checked == true)
            {
                numericUpDownPercent.Enabled = false;
                textBoxGigabytes.Enabled = false;
                labelPercentRemaining.Enabled = false;
            }
        }

        private void dataGridViewAdditionalOptions_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if ((epGigabytes.GetError(textBoxGigabytes) != "") ||
                (epPercent.GetError(numericUpDownPercent) != "") ||
                (epAdditionalOptions.GetError(dataGridViewAdditionalOptions) != ""))
            {
                MessageBox.Show(Properties.Resources.LogicalDriveSettings_Error_ErrorOnPage);
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void numericUpDownPercent_ValueChanged(object sender, EventArgs e)
        {
            decimal currentMax = numericUpDownPercent.Maximum - numericUpDownPercent.Value;
            labelPercentRemaining.Text = string.Format(Properties.Resources.LogicalDriveSettings_PercentRemaining, currentMax.ToString());
        }


        #endregion

        
    }
}
